import { Router } from "express"
import { IcallbackFn, Ioracle } from "../../types"

export default (router: Router, oracle: Ioracle, callbackFn: IcallbackFn) => {
  //主表
  //新增
  router.post("/addHeader", (req, res) => {
    const { org_code, server_ip, server_name, grade, purpose } = req.body
    const { manage_dept, manage_klm, manage_system, server_type } = req.body
    const { server_model, ilo_ip, host_server_name, host_server_ip } = req.body
    const { location1, location2, remarks } = req.body

    let sql = " insert into xx_server_manage_header "
    sql += " (org_code,server_ip,server_name,grade,purpose,manage_dept "
    sql += " ,manage_klm,manage_system,server_type,server_model "
    sql += " ,ilo_ip,host_server_name,host_server_ip "
    sql += " ,location1,location2,header_id,remarks) "
    sql += `values('${org_code}','${server_ip}','${server_name}','${grade}','${purpose}','${manage_dept}'`
    sql += `,'${manage_klm}','${manage_system}','${server_type}','${server_model}'`
    sql += `,'${ilo_ip}','${host_server_name}','${host_server_ip}','${location1}','${location2}' `
    sql += ` ,xx_server_manage_seq.nextval,'${remarks}')`

    callbackFn(res, oracle.connCcapps, { sql })
  })

  //查询
  router.post("/getHeader", (_req, res) => {
    let sql = " select a.*,a.location1||'-'||a.location2 location from xx_server_manage_header a "
    callbackFn(res, oracle.connCcapps, { sql })
  })

  //删除
  router.post("/delHeader", (req, res) => {
    const { header_id } = req.body
    let sql = " delete from xx_server_manage_header "
    sql += " where header_id = '" + header_id + "' "
    //sql += " and org_code = '"+ org_code +"' "
    callbackFn(res, oracle.connCcapps, { sql })
  })

  //修改
  router.post("/updHeader", (req, res) => {
    const { org_code, server_ip, server_name, grade, purpose } = req.body
    const { manage_dept, manage_klm, manage_system, server_type } = req.body
    const { server_model, ilo_ip, host_server_name, host_server_ip } = req.body
    const { location1, location2, header_id, remarks } = req.body

    let sql = " update xx_server_manage_header "
    sql += ` set server_name = '${server_name}' `
    sql += ` ,org_code = '${org_code}' `
    sql += ` ,grade = '${grade}' `
    sql += ` ,server_ip = '${server_ip}' `
    sql += ` ,purpose = '${purpose}' `
    sql += ` ,manage_dept = '${manage_dept}' `
    sql += ` ,manage_klm = '${manage_klm}' `
    sql += ` ,manage_system = '${manage_system}' `
    sql += ` ,server_type = '${server_type}' `
    sql += ` ,server_model = '${server_model}' `
    sql += ` ,ilo_ip = '${ilo_ip}' `
    sql += ` ,host_server_name = '${host_server_name}' `
    sql += ` ,host_server_ip = '${host_server_ip}' `
    sql += ` ,location1 = '${location1}' `
    sql += ` ,location2 = '${location2}' `
    sql += ` ,remarks = '${remarks}' `
    sql += ` where header_id = '${header_id}' `
    callbackFn(res, oracle.connCcapps, { sql })
  })

  //备份表
  //新增
  router.post("/addLine", (req, res) => {
    const { server_name, backup_tool, schedule_name } = req.body
    const { backup_file_name, backup_destination, remarks } = req.body
    const { backup_date1, backup_time1, backup_date2, backup_time2 } = req.body

    let sql = " declare "
    sql += " v_header_id varchar2(10); "
    sql += " begin "
    sql += " select header_id into v_header_id from xx_server_manage_header "
    sql += ` where server_name = '${server_name}'; `
    sql += " insert into xx_server_manage_line "
    sql += " (header_id,backup_tool,schedule_name "
    sql += " ,backup_file_name,backup_destination,remarks "
    sql += " ,backup_date1,backup_time1,backup_date2,backup_time2,line_id) "
    sql += `values(v_header_id,'${backup_tool}','${schedule_name}' `
    sql += `,'${backup_file_name}','${backup_destination}','${remarks}' `
    sql += `,'${backup_date1}','${backup_time1}','${backup_date2}','${backup_time2}',xx_server_manage_seq.nextval); `
    sql += " end; "

    callbackFn(res, oracle.connCcapps, { sql })
  })

  //查询
  router.post("/getLine", (_req, res) => {
    let sql = " select a.org_code,a.server_ip,a.server_name,b.backup_tool "
    sql +=
      " ,b.schedule_name,b.backup_file_name,b.backup_destination,b.remarks "
    sql += " ,b.backup_date1,b.backup_date2,b.backup_time1,b.backup_time2 "
    sql += " ,b.line_id,to_char(b.max_time,'yyyy/mm/dd hh24:mi:ss') max_time "
    // sql += " ,replace(replace(replace(replace(replace(replace(replace(b.backup_date1 "
    // sql += " ,'1','星期一'),'2','星期二'),'3','星期三') "
    // sql += " ,'4','星期四'),'5','星期五') "
    // sql += " ,'6','星期六'),'7','星期天') backup_date1 "
    // sql += " ,b.backup_time1||':00' backup_time1 "
    // sql += " ,replace(replace(replace(replace(replace(replace(replace(b.backup_date2 "
    // sql += " ,'1','星期一'),'2','星期二'),'3','星期三') "
    // sql += " ,'4','星期四'),'5','星期五') "
    // sql += " ,'6','星期六'),'7','星期天') backup_date2 "
    // sql += " ,b.backup_time2||':00' backup_time2 "
    //sql += " ,decode(b.backup_date1,'1','星期一','2','星期二','3','星期三','4','星期四','5','星期五','6','星期六','7','星期天',b.backup_date1)||backup_time1 backup_date1 "
    //sql += " ,decode(b.backup_date2,'1','星期一','2','星期二','3','星期三','4','星期四','5','星期五','6','星期六','7','星期天',b.backup_date2)||backup_time2 backup_date2 "
    //sql += " ,b.backup_time1,b.backup_time2 "
    sql += " from xx_server_manage_header a, xx_server_manage_line b"
    sql += " where a.header_id = b.header_id "
    callbackFn(res, oracle.connCcapps, { sql })
  })

  //修改
  router.post("/updLine", (req, res) => {
    const { line_id, backup_tool, schedule_name, backup_file_name } = req.body
    const { backup_destination, remarks, backup_date1 } = req.body
    const { backup_time1, backup_date2, backup_time2 } = req.body
    let sql = " update xx_server_manage_line "
    sql += " set backup_tool = '" + backup_tool + "' "
    sql += " ,schedule_name = '" + schedule_name + "' "
    sql += " ,backup_file_name = '" + backup_file_name + "' "
    sql += " ,backup_destination = '" + backup_destination + "' "
    sql += " ,remarks = '" + remarks + "' "
    sql += " ,backup_date1 = '" + backup_date1 + "' "
    sql += " ,backup_time1 = '" + backup_time1 + "' "
    sql += " ,backup_date2 = '" + backup_date2 + "' "
    sql += " ,backup_time2 = '" + backup_time2 + "' "
    sql += " where line_id = '" + line_id + "' "
    //sql += " and org_code = '"+ org_code +"' "
    callbackFn(res, oracle.connCcapps, { sql })
  })

  //删除
  router.post("/delLine", (req, res) => {
    const { line_id } = req.body
    let sql = " delete from xx_server_manage_line "
    sql += " where line_id = '" + line_id + "' "
    //sql += " and org_code = '"+ org_code +"' "
    callbackFn(res, oracle.connCcapps, { sql })
  })

  return router
}
